//
//  MessageViewController.swift
//  Swift_SendMessage
//
//  Created by lanou3g on 15/11/25.
//  Copyright © 2015年 笨蛋一号后卫. All rights reserved.
//

import UIKit

class MessageViewController: UIViewController,UITableViewDelegate, UITableViewDataSource{
//在sWift的项目中最好使用OC的数组
    var dataArray : NSMutableArray = NSMutableArray()
    @IBOutlet weak var tableView: UITableView!
    override func viewDidLoad() {
        
        super.viewDidLoad()
        self.tableView.estimatedRowHeight = 100
        self.tableView.rowHeight = UITableViewAutomaticDimension
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发微博", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("sendeBarButtonClicked"))
        self.loadData()
    }
    func sendeBarButtonClicked() {
        let sendVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("sendVC") as! SendViewController
        sendVC.result = { () -> Void in
        self.loadData()
        }
        self.navigationController?.pushViewController(sendVC, animated: true)
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("messageCell", forIndexPath: indexPath) as! MessageTableViewCell
        let object = self.dataArray[indexPath.row] as! PFObject
        
        cell.label.text = object.objectForKey("message") as? String
        return cell
    }
    func loadData() {
        let quary = PFQuery(className: "yan30Message")
        quary.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
            if error == nil {
                self.dataArray .setArray(objects!)
                self.tableView.reloadData()
            }
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
//    override func viewWillAppear(animated: Bool) {
//        super.viewWillAppear(true)
//        self.tableView.reloadData()
//    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
